package com.caiwangdai.app.extension

import android.os.Handler
import android.view.View
import android.widget.Toast
import com.caiwangdai.app.AppData


//吐司通知-普通
fun Any.toast(message: CharSequence) {
    Toast.makeText(AppData.context, message, Toast.LENGTH_SHORT).show()
}

//吐司通知-长
fun Any.toastLong(message: CharSequence) {
    Toast.makeText(AppData.context, message, Toast.LENGTH_LONG).show()
}

//隐藏所有视图  无占位
fun Any.hideAllViews(vararg views: View) {
    for (view in views) {
        view.visibility = View.GONE
    }
}


//隐藏所有视图  有占位
fun Any.inVisibleAllViews(vararg views: View) {
    for (view in views) {
        view.visibility = View.INVISIBLE
    }
}


//显示所有视图
fun Any.showAllViews(vararg views: View) {
    for (view in views) {
        view.visibility = View.VISIBLE
    }
}

//显示视图 one by one
fun Any.showAllViewOneByOne(timeMillis: Long, vararg views: View) {
    if (views.size < 1) return
    views[0].visibility = View.VISIBLE
    for (i in 1..views.size - 1) {
        val view = views[i]
        Handler().postDelayed({ view.visibility = View.VISIBLE }, timeMillis!! * i)
    }
}

private var clickTime: Long = 0
//双击
fun Any.doubleClick(doubleClick: () -> Unit, firstClick: () -> Unit={}, delay: Long = 2000) {
    if (System.currentTimeMillis() - clickTime > delay) {
        firstClick.invoke()
        clickTime = System.currentTimeMillis()
    } else {
        doubleClick.invoke()
    }
}
